//
//  StoreRequestManager.m
//  much
//
//  Created by weijunjie on 2019/1/4.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "MuchStoreRequestManager.h"
#import "GoodsModel.h"
#import "GoodsStandardModel.h"
#import "SearchFilterModel.h"
#import "OrderDataModel.h"


@implementation MuchStoreRequestManager
+ (void)storeSearchWithFilterModel:(SearchFilterModel *)filterModel
                  successBlock:(nullable RequestSuccessAndTotalPage)successBlock
                  failureBlock:(nullable RequestFailed)failureBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (filterModel.keyWord.hasValue) [param setObject:filterModel.keyWord forKey:@"keyWord"];
    if (filterModel.orderDir.hasValue) [param setObject:filterModel.orderDir forKey:@"orderDir"];
    if (filterModel.orderField.hasValue) [param setObject:filterModel.orderField forKey:@"orderField"];
    [param setObject:@(filterModel.pageIndex) forKey:@"pageIndex"];
    
    [param setObject:@(true) forKey:@"debug"];
    [MuchHttpRequestManager sendHttpRequestWithAPI:StoreSearchAPI method:HttpRequestGet arguments:param andSuccessCallback:^(id responseObject) {
        NSArray *arr = [NSArray modelArrayWithClass:[GoodsModel class] json:responseObject[@"content"]];
        if (successBlock) {
            successBlock(arr,[responseObject[@"totalPages"] integerValue]);
        }
    } andFailCallback:^(NSString *error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

+ (void)storeDetailInfoWithSpuSid:(NSString *)spuSid
                     successBlock:(nullable RequestSuccess)successBlock
                     failureBlock:(nullable RequestFailed)failureBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(true) forKey:@"debug"];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",StoreDetailInfoAPI,spuSid];
    [MuchHttpRequestManager sendHttpRequestWithAPI:urlStr method:HttpRequestGet arguments:param andSuccessCallback:^(id responseObject) {
        GoodsModel *model = [GoodsModel modelWithDictionary:responseObject];
        if (successBlock) {
            successBlock(model);
        }
    } andFailCallback:^(NSString *error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}


+ (void)storeDetailStandardInfoWithSpuSid:(NSString *)spuSid
                             successBlock:(nullable RequestSuccess)successBlock
                             failureBlock:(nullable RequestFailed)failureBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(true) forKey:@"debug"];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/sku",StoreDetailInfoAPI,spuSid];
    [MuchHttpRequestManager sendHttpRequestWithAPI:urlStr method:HttpRequestGet arguments:param andSuccessCallback:^(id responseObject) {
        GoodsStandardModel *standardModel = [GoodsStandardModel modelWithDictionary:responseObject];
        if (successBlock) {
            successBlock(standardModel);
        }
    } andFailCallback:^(NSString *error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

+ (void)storeGoodsPreOrderWithAmount:(NSString *)amount
                          goodsArray:(NSArray *)goodsArray
                        successBlock:(nullable RequestSuccess)successBlock
                        failureBlock:(nullable RequestFailed)failureBlock {
    
    NSMutableDictionary * pagram = [[NSMutableDictionary alloc] init];
    [pagram setObject:amount forKey:@"amount"];
    NSMutableArray * spus = [[NSMutableArray alloc] init];
    [goodsArray enumerateObjectsUsingBlock:^(GoodsModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
        if (obj.amount.hasValue) [dict setObject:obj.amount forKey:@"amount"];
         [dict setObject:@(obj.number) forKey:@"number"];
        if (obj.sid.hasValue) [dict setObject:obj.sid forKey:@"sid"];
        if (obj.skuSid.hasValue) [dict setObject:obj.skuSid forKey:@"skuSid"];
        if (obj.spuSid.hasValue) [dict setObject:obj.spuSid forKey:@"spuSid"];
        if (obj.type.hasValue) [dict setObject:obj.type forKey:@"type"];
        if (obj.version.hasValue) [dict setObject:obj.version forKey:@"version"];
        [spus addObject:dict];
    }];
    
    [pagram setObject:spus forKey:@"spus"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:pagram options:NSJSONWritingPrettyPrinted error:nil];
    [MuchHttpRequestManager sendHttpRequestWithAPI:GoodsOrderBefore method:HttpRequestPost arguments:jsonData andSuccessCallback:^(id responseObject) {
        
        OrderDataModel *dataModel = [OrderDataModel modelWithDictionary:responseObject];
        if (successBlock) {
            successBlock(dataModel);
        }
        
    } andFailCallback:^(NSString *error) {
        if (failureBlock) {
            failureBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeGetProvinceList:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSString *requestUrl = [NSString stringWithFormat:@"%@", ProvinceListAPI];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestGet arguments:nil andSuccessCallback:^(id responseObject) {
        NSArray *arr = [NSArray modelArrayWithClass:[ProvinceModel class] json:responseObject];
        if (successBlock) {
            successBlock(arr);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeGetCityList:(NSString *)parentSid success:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSString *requestUrl = [NSString stringWithFormat:@"%@/%@", CityListAPI, parentSid];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestGet arguments:nil andSuccessCallback:^(id responseObject) {
        NSArray *arr = [NSArray modelArrayWithClass:[ProvinceModel class] json:responseObject[0][@"children"]];
        if (successBlock) {
            successBlock(arr);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeGetDistrictList:(NSString *)cityCode success:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSString *requestUrl = [NSString stringWithFormat:@"%@/%@", CityListAPI, cityCode];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestGet arguments:nil andSuccessCallback:^(id responseObject) {
        NSArray *arr = [NSArray modelArrayWithClass:[ProvinceModel class] json:responseObject[0][@"children"]];
        if (successBlock) {
            successBlock(arr);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeGetAddressList:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSString *requestUrl = [NSString stringWithFormat:@"%@", ShoppingAddressListAPI];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestGet arguments:nil andSuccessCallback:^(id responseObject) {
        NSArray *arr = [NSArray modelArrayWithClass:[StoreAddressModel class] json:responseObject];
        if (successBlock) {
            successBlock(arr);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeDeleteAddress:(NSString *)sid success:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSString *requestUrl = [NSString stringWithFormat:@"%@/%@", DeleteEditShoppingAddressListAPI, sid];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestDelete arguments:nil andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeEditAddress:(NSString *)sid areaSid:(NSString *)areaSid address:(NSString *)address name:(NSString *)name phone:(NSString *)phone addressDefault:(BOOL)addressDefault sucess:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSMutableDictionary * pagram = [[NSMutableDictionary alloc] init];
    [pagram setObject:areaSid forKey:@"districtSid"];
    [pagram setObject:address forKey:@"address"];
    [pagram setObject:name forKey:@"name"];
    [pagram setObject:phone forKey:@"mobile"];
    [pagram setObject:@(addressDefault) forKey:@"defaultSetting"];
    
    NSString *pagramStr = [NSString dictionaryToJSONString:pagram];
    NSData *data = [pagramStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *requestUrl = [NSString stringWithFormat:@"%@/%@", DeleteEditShoppingAddressListAPI, sid];
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestPut arguments:data andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)storeSaveAddress:(NSString *)areaSid address:(NSString *)address name:(NSString *)name phone:(NSString *)phone addressDefault:(BOOL)addressDefault sucess:(RequestSuccess)successBlock fail:(RequestFailed)failBlock {
    NSMutableDictionary * pagram = [[NSMutableDictionary alloc] init];
    [pagram setObject:areaSid forKey:@"districtSid"];
    [pagram setObject:address forKey:@"address"];
    [pagram setObject:name forKey:@"name"];
    [pagram setObject:phone forKey:@"mobile"];
    [pagram setObject:@(addressDefault) forKey:@"defaultSetting"];
    
    NSString *pagramStr = [NSString dictionaryToJSONString:pagram];
    NSData *data = [pagramStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *requestUrl = [NSString stringWithFormat:@"%@", DeleteEditShoppingAddressListAPI];
    [MuchHttpRequestManager sendHttpRequestWithAPI:requestUrl method:HttpRequestPost arguments:data andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

@end
